const db = wx.cloud.database();
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 0,
    index2: 0,
    index3: 0,
    index4: 0,
    shi: ['请选择地区'],
    xianqu: ['请选择报考县区'],
    suoshudanwei: ['请选择所属单位'],
    gangweibianma: ['请选择岗位编码'],
    name: '',
    fenshu: '',
    phone: 0,
    theList: ''
  },

  nameInput: function (e) {
    this.setData({
      name: e.detail.value
    })
    console.log(this.data.name)
  },
  fenshuInput: function (e) {
    this.setData({
      fenshu: e.detail.value
    })
    console.log(this.data.fenshu)
  },

  bindPickerChange: function (e) {
    let num = e.detail.value;
    let that = this;
    console.log('shi发送选择改变，携带值为', that.data.shi[num]);
    this.setData({
      index: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=41444&callback=?', //接收url
      data: {
        level: '2',
        grfiled: 'shi',
        grtext: that.data.shi[num]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择报考县区')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].xianqu)
        }
        that.setData({
          xianqu: list
        });
      }
    })
  },
  bindPickerChange2: function (e) {
    let num2 = e.detail.value;
    let that = this;
    console.log('xianqu发送选择改变，携带值为', that.data.xianqu[num2]);
    this.setData({
      index2: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=41444&callback=?', //接收url
      data: {
        level: '3',
        grfiled: 'xianqu',
        grtext: that.data.xianqu[num2],
        onefiled: 'shi',
        onetext: that.data.shi[this.data.index]
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson)
        var list = [];
        list.push('请选择所属单位')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].suoshudanwei)

        }
        that.setData({
          suoshudanwei: list
        });
      }
    })
  },
  bindPickerChange3: function (e) {
    let num3 = e.detail.value;
    let that = this;
    console.log('suoshudanwei发送选择改变，携带值为', that.data.suoshudanwei[num3]);
    this.setData({
      index3: e.detail.value
    })
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=41444&callback=?', //接收url
      data: {
        level: '4',
        grfiled: 'suoshudanwei',
        grtext: that.data.suoshudanwei[num3],
        onefiled: 'shi',
        onetext: that.data.shi[this.data.index],
        twofiled: 'xianqu',
        twotext: that.data.xianqu[this.data.index2],
      }, //接收参数
      success(res) {
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        console.log(theJson.lists)
        var list = [];
        list.push('请选择岗位编码')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].gangweibianma)
        }
        that.setData({
          gangweibianma: list
        });
      }
    })
  },
  bindPickerChange4: function (e) {
    let num4 = e.detail.value;
    let that = this;
    console.log('gangweibianma发送选择改变，携带值为', that.data.gangweibianma[num4]);
    this.setData({
      index4: e.detail.value
    })
  },


  onLoad: function (options) {
    var that = this;
    wx.request({ //发送请求
      url: 'https://zg99.offcn.com/index/shaifen/getlevel?actid=41444&callback=?', //接收url
      data: {
        level: '1',
        shi: ''
      }, //接收参数
      success(res) { //返回
        var jsonStr = res.data;
        jsonStr = jsonStr.replace("(", "");
        jsonStr = jsonStr.replace(")", "");
        if (typeof jsonStr != 'object') {
          jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
          var theJson = JSON.parse(jsonStr);
        }
        var list = [];
        list.push('请选择报考地市')
        for (var i in theJson.lists) { //循环写入下来框
          list.push(theJson.lists[i].shi)
        }
        that.setData({
          shi: list
        });
      }
    })
    var that = this;
    console.log(options);
    that.setData({
      area: options.area,
      geneal: options.agent
    })
    if (options.page) {
      that.setData({
        page: options.page
      })
    }
  },

  b1: function () {
    wx.navigateTo({
      url: '../list/list',
    })
  },

  getPhoneNumber(e) {
    var that = this;
    var shi = that.data.shi[that.data.index];
    var xianqu = that.data.xianqu[that.data.index2];
    var suoshudanwei = that.data.suoshudanwei[that.data.index3];
    var gangweibianma = that.data.gangweibianma[that.data.index4];
    var fenshu = that.data.fenshu;

    wx.cloud.callFunction({
      name: 'getMobile',
      data: {
        weRunData: wx.cloud.CloudID(e.detail.cloudID),
      }
    }).then(res => {

      wx.request({
        url: 'https://tgadmin.sdwx.zgkfpt.cn/api/v1/collect_v3',
        method: 'POST',
        data: {
          activeId: 7,
          mobile: res.result,
          areaCode: that.data.area,
          agent: that.data.geneal,
          pageId: that.data.page
        },
        success: res => {
          console.log(res);
        }
      })
      
      var fenshuSt = {
        shi: shi,
        xianqu: xianqu,
        suoshudanwei: suoshudanwei,
        gangweibianma: gangweibianma,
        fenshu: fenshu,
        phone: res.result
      }
      wx.setStorageSync('fenshuSt', fenshuSt)
      console.log(res.result)
      var phone = res.result;
      wx.request({ //发送请求
        url: 'https://zg99.offcn.com/index/shaifen/register?actid=41444&callback=?', //接收url
        data: {
          shi: shi,
          xianqu: xianqu,
          suoshudanwei: suoshudanwei,
          gangweibianma: gangweibianma,
          fenshu: fenshu,
          phone: res.result,
          fenxiao: that.data.area,
          geneal: that.data.geneal,
        }, //接收参数
        success(res) {
          var jsonStr = res.data;
          jsonStr = jsonStr.replace("(", "");
          jsonStr = jsonStr.replace(")", "");
          if (typeof jsonStr != 'object') {
            jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
            var theJson = JSON.parse(jsonStr);
          }
          console.log(theJson)

          wx.request({ //发送请求
            url: 'https://zg99.offcn.com/index/shaifen/getphoneinfo?actid=41444&callback=?', //接收url
            data: {
              phone: phone,
              page: 1,
              limit: 20
            }, //接收参数
            success(res) {
              var jsonStr = res.data;
              jsonStr = jsonStr.replace("(", "");
              jsonStr = jsonStr.replace(")", "");
              if (typeof jsonStr != 'object') {
                jsonStr = jsonStr.replace(/\ufeff/g, ""); //字符串转化JSON对象
                var theJson = JSON.parse(jsonStr);
              }
              console.log(theJson)

              that.setData({
                paihang: theJson.lists[0].paihang,
                fenshu: theJson.lists[0].fenshu,
                fixed: 1
              })
            }
          })

        }
      })
    }).catch(err => {
      console.error(err);
    });

    
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})